﻿using MetaProperties.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading;

namespace MetaProperties.Core.Tests
{
    
    
    /// <summary>
    ///This is a test class for SynchronizationContextContainerExtensionsTest and is intended
    ///to contain all SynchronizationContextContainerExtensionsTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SynchronizationContextContainerExtensionsTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Send
        ///</summary>
        [TestMethod()]
        public void SendTest2()
        {
            TestSyncContext context = new TestSyncContext();
            ISynchronizationContextContainer container = new SynchronizationContextContainer(context);

            bool callbackCalled = false;
            Action d = () => callbackCalled = true;

            bool isSendCalled = false;
            bool isPostCalled = false;
            
            context.SendCalled += (s, e) => isSendCalled = true;
            context.PostCalled += (s, e) => isPostCalled = true;

            SynchronizationContextContainerExtensions.Send(container, d);

            Assert.IsTrue(callbackCalled);
            Assert.IsTrue(isSendCalled);
            Assert.IsFalse(isPostCalled);
        }

        /// <summary>
        ///A test for Send
        ///</summary>
        [TestMethod()]
        public void SendTest1()
        {
            TestSyncContext context = new TestSyncContext();
            ISynchronizationContextContainer container = new SynchronizationContextContainer(context);

            bool callbackCalled = false;
            SendOrPostCallback d = o => callbackCalled = true;

            bool isSendCalled = false;
            bool isPostCalled = false;
            
            context.SendCalled += (s, e) => isSendCalled = true;
            context.PostCalled += (s, e) => isPostCalled = true;

            SynchronizationContextContainerExtensions.Send(container, d, null);

            Assert.IsTrue(callbackCalled);
            Assert.IsTrue(isSendCalled);
            Assert.IsFalse(isPostCalled);
        }

        /// <summary>
        ///A test for Send
        ///</summary>
        [TestMethod()]
        public void SendTest()
        {
            TestSyncContext context = new TestSyncContext();
            ISynchronizationContextContainer container = new SynchronizationContextContainer(context);

            bool callbackCalled = false;
            Action d = () => callbackCalled = true;

            bool isSendCalled = false;
            bool isPostCalled = false;

            context.SendCalled += (s, e) => isSendCalled = true;
            context.PostCalled += (s, e) => isPostCalled = true;

            SynchronizationContextContainerExtensions.Send(context, d);

            Assert.IsTrue(callbackCalled);
            Assert.IsTrue(isSendCalled);
            Assert.IsFalse(isPostCalled);
        }

        /// <summary>
        ///A test for Post
        ///</summary>
        [TestMethod()]
        public void PostTest2()
        {
            TestSyncContext context = new TestSyncContext();
            ISynchronizationContextContainer container = new SynchronizationContextContainer(context);

            bool callbackCalled = false;
            SendOrPostCallback d = o => callbackCalled = true;

            bool isSendCalled = false;
            bool isPostCalled = false;

            context.SendCalled += (s, e) => isSendCalled = true;
            context.PostCalled += (s, e) => isPostCalled = true;

            SynchronizationContextContainerExtensions.Post(container, d, null);

            Assert.IsTrue(callbackCalled);
            Assert.IsFalse(isSendCalled);
            Assert.IsTrue(isPostCalled);
        }

        /// <summary>
        ///A test for Post
        ///</summary>
        [TestMethod()]
        public void PostTest1()
        {
            TestSyncContext context = new TestSyncContext();
            ISynchronizationContextContainer container = new SynchronizationContextContainer(context);

            bool callbackCalled = false;
            Action d = () => callbackCalled = true;

            bool isSendCalled = false;
            bool isPostCalled = false;

            context.SendCalled += (s, e) => isSendCalled = true;
            context.PostCalled += (s, e) => isPostCalled = true;

            SynchronizationContextContainerExtensions.Post(context, d);

            Assert.IsTrue(callbackCalled);
            Assert.IsFalse(isSendCalled);
            Assert.IsTrue(isPostCalled);
        }

        /// <summary>
        ///A test for Post
        ///</summary>
        [TestMethod()]
        public void PostTest()
        {
            TestSyncContext context = new TestSyncContext();
            ISynchronizationContextContainer container = new SynchronizationContextContainer(context);

            bool callbackCalled = false;
            Action d = () => callbackCalled = true;

            bool isSendCalled = false;
            bool isPostCalled = false;

            context.SendCalled += (s, e) => isSendCalled = true;
            context.PostCalled += (s, e) => isPostCalled = true;

            SynchronizationContextContainerExtensions.Post(container, d);

            Assert.IsTrue(callbackCalled);
            Assert.IsFalse(isSendCalled);
            Assert.IsTrue(isPostCalled);
        }

        private class TestSyncContext : SynchronizationContext
        {
            public event EventHandler PostCalled;

            public event EventHandler SendCalled;

            public override void Post(SendOrPostCallback d, object state)
            {
                var handler = this.PostCalled;
                if (handler != null)
                {
                    handler(this, EventArgs.Empty);
                }
                d(state);
            }

            public override void Send(SendOrPostCallback d, object state)
            {
                var handler = this.SendCalled;
                if (handler != null)
                {
                    handler(this, EventArgs.Empty);
                }
                d(state);
            }
        }
    }
}
